//
//  ViewController.m
//  whfdemo004
//
//  Created by 王洪飞 on 2024/6/14.
//

#import "ViewController.h"
#import <OpenGLES/ES3/gl.h>
#import <OpenGLES/ES3/glext.h>
#import "Render.h"
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"

@interface ViewController ()
@property (nonatomic, strong) EAGLContext *mContext;
@property (nonatomic, strong) Render *render;
@property (nonatomic, strong) UISwitch *mBaseSwitch;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.mContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3];
    GLKView *view = (GLKView *)self.view;
    view.context = self.mContext;
    [EAGLContext setCurrentContext:self.mContext];
    self.render = [[Render alloc] initWidth:self.view.bounds.size.width height:self.view.bounds.size.height];
    [self.view addSubview:self.mBaseSwitch];
}

- (UISwitch *)mBaseSwitch{
    if (_mBaseSwitch == nil) {
        _mBaseSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(50, 100, 100, 30)];
    }
    return _mBaseSwitch;
}

- (void)update{
    NSLog(@"update");
    [self.render update:self.mBaseSwitch.on];
}

-(void)glkView:(GLKView *)view drawInRect:(CGRect)rect{
//    NSLog(@"draw");
    glViewport(0, 0, view.drawableWidth, view.drawableHeight);
    glClearColor(0.5, 0.5, 1, 1);
    glClear(GL_COLOR_BUFFER_BIT);

    [self.render draw];
    
}

@end
